﻿using A.M.ETO;
using AutoMapper;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Newtonsoft.Json;
using Public_Class_Library;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Application.Services;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using Q.M.Finance.Infrastruecure.Repository;
using System.Drawing.Printing;
using System.Linq.Expressions;
using W.M.Materiel.Domain.Entity;

namespace Q.M.Finance.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExpenditureWriteController : ControllerBase
    {
        private readonly IExpenditureServices expenditureServices;
        public ExpenditureWriteController(IExpenditureServices expenditureServices)
        {
            this.expenditureServices = expenditureServices;
        }
        /// <summary>
        /// 添加支出管理
        /// </summary>
        /// <param name="expenditureAddDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetexpenditureAddInfo(expenditureAddDTO expenditureAddDTO)
        {
            try
            {
                var res=await expenditureServices.GetexpenditureAddInfo(expenditureAddDTO);
                return Ok(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改支出管理
        /// </summary>
        /// <param name="expenditureShowDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> GetexpenditureUpdateInfo(expenditureShowDTO expenditureShowDTO)
        {
            var res = await expenditureServices.GetexpenditureUpdateInfo(expenditureShowDTO);
            return Ok(res);
        }
        /// <summary>
        /// 反填支出管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FindexpenditureInfo(string? invoiceid)
        {
            var res = await expenditureServices.FindexpenditureInfo(invoiceid);
            return Ok(res);
        }

        /// <summary>
        /// 删除支出管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Deleteexpenditure(string? invoiceid)
        {
            var res = await expenditureServices.Deleteexpenditure(invoiceid);
            return Ok(res);
        }
        /// <summary>
        /// 支出管理显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetexpenditureInfo(string? invoiceid, int pageIndex, int pageSize)
        {
           var res = expenditureServices.GetexpenditureInfo(invoiceid, pageIndex, pageSize);
            return Ok(res);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportTiExcel()
        {
            var list = expenditureServices.GetShowInfor();
            var steram = new MemoryStream();
            steram.SaveAs(list);
            steram.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(steram, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "支出信息.xlsx"
            };
        }

    }
}
